<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0 ,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="description" content="{{env('SITE_DESCRIPTION')}}">
    <title>{{env('SITE_TITLE')}}</title>
    <link href="{{asset('/org/site')}}/AmazeUI-2.4.2/assets/css/amazeui.css" rel="stylesheet" type="text/css"/>
    <link href="{{asset('/org/site')}}/basic/css/demo.css" rel="stylesheet" type="text/css"/>
    <link href="{{asset('/org/site')}}/css/cartstyle.css" rel="stylesheet" type="text/css"/>
    <link href="{{asset('/org/site')}}/css/jsstyle.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="{{asset('/org/site')}}/js/address.js"></script>
    {{--
    <meta name="csrf-token" content="{{ csrf_token() }}">
    $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
    --}}
    @include('admin.layouts._hdjs')
    @include('admin.layouts._message')
</head>
<body>
<!--网站公共顶部 -->
@include('site.layouts._top')
<div class="clear"></div>
<div class="concent">
    <!--地址 -->
    <div class="paycont">
        <div class="address">
            <h3>确认收货地址 </h3>
            <div class="control">
                <div class="tc-btn createAddr theme-login am-btn am-btn-danger">使用新地址</div>
            </div>
            <div class="clear"></div>
            <ul id="address-list">
                @foreach($address as $add)
                    <div class="per-border"></div>
                    {{--给循环的每个li标签加一个自定义属性,方便后面点击的时候更新页面底部的地址--}}
                    <li address-num="{{$add['id']}}" onclick="topClick(this)"
                        class="user-addresslist {{$add['is_default']?'defaultAddr':''}}">
                        <div class="address-left">
                            <div class="user DefaultAddr">
							<span class="buy-address-detail">
                   <span id="top-name" class="buy-user">{{$add['name']}} </span>
					<span id="top-phone" class="buy-phone">
                        {{$add['phone']}}
                    </span>
							</span>
                            </div>
                            <div class="default-address DefaultAddr">
                                <span class="buy-line-title buy-line-title-type">收货地址：</span>
                                <span class="buy--address-detail">
								   <span id="top-area" class="province">{{$add['area']}}</span>
									<span id="top-detail" class="street">
                                        {{$add['detail']}}
                                    </span>
								</span>
                                </span>
                            </div>
                            <ins class="deftip hidden">默认地址</ins>
                        </div>
                        <div class="address-right">
                            <a href="{{asset('/org/site')}}/person/address.html">
                                <span class="am-icon-angle-right am-icon-lg"></span></a>
                        </div>
                        <div class="clear"></div>

                        <div class="new-addr-btn">
                            {{--
                            <a href="#" class="hidden">设为默认</a>
                            <span class="new-addr-bar hidden">|</span>
                            <a href="#">编辑</a>
                            <span class="new-addr-bar">|</span>
                            <a href="javascript:void(0);" onclick="delClick(this);">删除</a>
                            --}}
                        </div>

                    </li>
                @endforeach
            </ul>
            <script>
                //给上面的循环的li标签加点击事件
                function topClick(obj) {
                    //通过获取li中的address-num这个自定义属性的值可以知道当前选中的收货地址
                    // alert($(obj).attr("address-num"))
                    //通过text()可以获取到当前选中的收货地址的信息
                    // alert($(obj).find('#top-detail').text())
                    // 重新设置底部对应地址信息
                    let area = $(obj).find('#top-area').text();
                    let detail = $(obj).find('#top-detail').text();
                    let name = $(obj).find('#top-name').text();
                    let phone = $(obj).find('#top-phone').text();
                    // 将用户重新选中的地址的信息赋值到页面底部的对应位置
                    $('#bottom-area').text(area);
                    $('#bottom-detail').text(detail);
                    $('#bottom-name').text(name);
                    $('#bottom-phone').text(phone);
                }
            </script>
            <div class="clear"></div>
        </div>
        <!--物流 -->
        <div class="logistics">
            <h3>选择物流方式</h3>
            <ul class="op_express_delivery_hot">
                <li data-value="yuantong" class="OP_LOG_BTN  "><i class="c-gap-right"
                                                                  style="background-position:0px -468px"></i>圆通<span></span>
                </li>
                <li data-value="shentong" class="OP_LOG_BTN  "><i class="c-gap-right"
                                                                  style="background-position:0px -1008px"></i>申通<span></span>
                </li>
                <li data-value="yunda" class="OP_LOG_BTN  "><i class="c-gap-right"
                                                               style="background-position:0px -576px"></i>韵达<span></span>
                </li>
                <li data-value="zhongtong" class="OP_LOG_BTN  "><i class="c-gap-right"
                                                                                               style="background-position:0px -324px"></i>中通<span></span>
                </li>
                <li data-value="shunfeng" class="OP_LOG_BTN  "><i class="c-gap-right"
                                                                                                style="background-position:0px -180px"></i>顺丰<span></span>
                </li>
            </ul>
        </div>
        <div class="clear"></div>

        <!--支付方式-->
        <div class="logistics">
            <h3>选择支付方式</h3>
            <ul class="pay-list">
                <li class="pay card"><img src="{{asset('/org/site')}}/images/wangyin.jpg"/>银联<span></span></li>
                <li class="pay qq"><img src="{{asset('/org/site')}}/images/weizhifu.jpg"/>微信<span></span></li>
                <li class="pay taobao"><img src="{{asset('/org/site')}}/images/zhifubao.jpg"/>支付宝<span></span></li>
            </ul>
        </div>
        <div class="clear"></div>

        <!--订单 -->
        <div class="concent">
            <div id="payTable">
                <h3>确认订单信息</h3>
                <div class="cart-table-th">
                    <div class="wp">

                        <div class="th th-item">
                            <div class="td-inner">商品信息</div>
                        </div>
                        <div class="th th-price">
                            <div class="td-inner">单价</div>
                        </div>
                        <div class="th th-amount">
                            <div class="td-inner">数量</div>
                        </div>
                        <div class="th th-sum">
                            <div class="td-inner">金额</div>
                        </div>
                        <div class="th th-oplist">
                            <div class="td-inner">配送方式</div>
                        </div>

                    </div>
                </div>
                <div class="clear"></div>
            </div>
            {{--循环输出订单信息--}}
            @foreach($carts as $cart)
                <tr class="item-list">
                    <div id="J_Bundle_s_1911116345_1_0" class="bundle  bundle-last">
                        <div class="bundle-main">
                            <ul class="item-content clearfix">
                                <div class="pay-phone">
                                    <li class="td td-item">
                                        <div class="item-pic">
                                            <a href="#" class="J_MakePoint">
                                                <img src="{{$cart->goods->pics[0]}}" width="80px"
                                                     class="itempic J_ItemImg"></a>
                                        </div>
                                        <div class="item-info">
                                            <div class="item-basic-info">
                                                <a href="{{route('site.detail',['goods_id'=>$cart['goods_id'],'product_id'=>$cart['product_id']])}}"
                                                   target="_blank" title="{{$cart->goods->title}}"
                                                   class="item-title J_MakePoint"
                                                   data-point="tbcart.8.11">{{$cart->goods->title}}
                                                </a>
                                            </div>
                                        </div>
                                    </li>
                                    <li class="td td-info">
                                        <div class="item-props">
                                        <span class="sku-line">
                                            {{$cart->product->attrs}}
                                        </span>
                                            <span class="sku-line hidden">
                                            包装：两支手袋装（送彩带）
                                        </span>
                                        </div>
                                    </li>
                                    <li class="td td-price">
                                        <div class="item-price price-promo-promo">
                                            <div class="price-content">
                                                <em class="J_Price price-now">{{$cart->price}}</em>
                                            </div>
                                        </div>
                                    </li>
                                </div>

                                <li class="td td-amount">
                                    <div class="amount-wrapper ">
                                        <div class="item-amount ">
                                            <span class="phone-title">购买数量</span>
                                            <div class="sl">
                                                <input class="min am-btn hidden" name="" type="button" value="-"/>
                                                <input class="text_box" readonly type="text" value="{{$cart->num}}"
                                                       style="width:30px;text-align: center"/>
                                                <input class="add am-btn hidden" name="" type="button" value="+"/>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                                <li class="td td-sum">
                                    <div class="td-inner">
                                        <em tabindex="0" class="J_ItemSum number">{{$cart->sum}}</em>
                                    </div>
                                </li>
                                <li class="td td-oplist">
                                    <div class="td-inner">
                                        <span class="phone-title">配送方式</span>
                                        <div class="pay-logis">
                                            {{array_random(['包邮','看心情','倒贴'])}}
                                        </div>
                                    </div>
                                </li>

                            </ul>
                            <div class="clear"></div>
                        </div>
                    </div>
                </tr>
            @endforeach
        </div>
        <div class="clear"></div>
        <div class="pay-total">
            <!--留言-->
            <div class="order-extra">
                <div class="order-user-info">
                    <div id="holyshit257" class="memo">
                        <label>买家留言：</label>
                        <input type="text" title="选填,对本次交易的说明（建议填写已经和卖家达成一致的说明）" placeholder="选填,建议填写和卖家达成一致的说明"
                               class="memo-input J_MakePoint c2c-text-default memo-close">
                        <div class="msg hidden J-msg">
                            <p class="error">最多输入500个字符</p>
                        </div>
                    </div>
                </div>

            </div>
            <!--优惠券 -->
            <div class="buy-agio">
                <li class="td td-coupon">

                    <span class="coupon-title">优惠券</span>
                    {{--优惠券是一个select下拉选择框--}}
                    <select data-am-selected>
                        <option value="a" selected>
                            <div class="c-price">
                                <strong>￥0</strong>
                            </div>
                            <div class="c-limit">
                                【空空如也】
                            </div>
                        </option>
                        <option value="b" class="hidden">
                            <div class="c-price">
                                <strong>￥3</strong>
                            </div>
                            <div class="c-limit">
                                【无使用门槛】
                            </div>
                        </option>
                    </select>
                </li>

                <li class="td td-bonus">

                    <span class="bonus-title">红包</span>
                    <select data-am-selected>
                        <option value="a">
                            <div class="item-info">
                                ¥0.00<span>元</span>
                            </div>
                            <div class="item-remainderprice">
                                <span>还剩</span>0<span>元</span>
                            </div>
                        </option>
                        <option value="b" class="hidden">
                            <div class="item-info">
                                ¥50.00<span>元</span>
                            </div>
                            <div class="item-remainderprice">
                                <span>还剩</span>50.00<span>元</span>
                            </div>
                        </option>
                    </select>

                </li>

            </div>
            <div class="clear"></div>
        </div>
        <!--含运费小计 -->
        <div class="buy-point-discharge ">
            <p class="price g_price ">
                合计（含运费） <span>¥</span>
                <em class="pay-sum">
                    {{--
                    传递过来的$carts就是用户当前要结账的购物车中记录的集合对象,可调用集合的sum()方法,只是
                    因为$carts集合包含嵌套数组或对象，则应该传入一个键来指定要进行求和的值
                    --}}
                    {{$carts->sum('sum')}}
                </em>
            </p>
        </div>

        <!--信息 -->
        <div class="order-go clearfix">
            <div class="pay-confirm clearfix">
                <div class="box">
                    <div tabindex="0" id="holyshit267" class="realPay"><em class="t">实付款：</em>
                        <span class="price g_price ">
                                    <span>¥</span>
                            <em class="style-large-bold-red " id="J_ActualFee">
                                {{--算法给上面的合计算法一样--}}
                                {{$carts->sum('sum')}}
                            </em>
                        </span>
                    </div>

                    <div id="holyshit268" class="pay-address">

                        <p class="buy-footer-address">
                            <span class="buy-line-title buy-line-title-type">寄送至：</span>
                            <span class="buy--address-detail">
								   <span class="province" id="bottom-area">
                                       {{\App\Models\Address::where('is_default',1)->where('user_id',auth()->id())->value('area')}}
                                   </span>
									<span class="street" id="bottom-detail">
                                        {{\App\Models\Address::where('is_default',1)->where('user_id',auth()->id())->value('detail')}}
                                   </span>
                            </span>
                            </span>
                        </p>
                        <p class="buy-footer-address">
                            <span class="buy-line-title">收货人：</span>
                            <span class="buy-address-detail">   
                                         <span class="buy-user" id="bottom-name">
                                             {{$defaultAddress['name']}}
                                         </span>
												<span class="buy-phone" id="bottom-phone">
                                                    {{$defaultAddress['phone']}}
                                                </span>
                            </span>
                        </p>
                    </div>
                </div>

                <div id="holyshit269" class="submitOrder">
                    <div class="go-btn-wrap">
                        @if(request('cart_id'))
                            {{--a标签如果没有href属性那点击的时候也不会跳转--}}
                            <a id="J_Go" onclick="storeOrder()" class="btn-go" tabindex="0">
                                提交订单
                            </a>
                        @else
                            <a id="direct_store_order" class="btn-go" tabindex="0">
                                提交订单
                            </a>
                        @endif
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
    </div>
    <script>
        //通过购物车结算生成订单的时候执行这个方法
        function storeOrder() {
            // 这里需要写一个js实现用户点击"提交订单"按钮的时候需要选择快递和支付方式
            //按照下面的方式进行查找元素,如果没找length的值为0,找到为1
            let  item_kuaidi = $(".op_express_delivery_hot").find("li.selected").length;
            let item_pay = $(".pay-list").find("li.selected").length;
            // 如果快递或者支付方式任何一个没有选都不能提交
            if (item_kuaidi == 0 || item_pay == 0 ){
                require(['hdjs'],function (hdjs) {
                    hdjs.swal({
                        text:"请选择快递和支付方式",
                        button:false,
                        icon:'warning'
                    })
                })
                return false;
            }
            // 下面的jquery意思是在id为address-list的元素内部查找类名中有defaultAddr的里标签(li.defaultAddr),并获取这个标签中address-num这个标签属性的值
            // alert($('#address-list').find("li.defaultAddr").attr('address-num'));
            // 这里通过异步请求的方式将请求后台
            require(['hdjs', 'axios'], function (hdjs, axios) {
                // 先拼凑要发送到后台的数据
                let addressTo = $('#address-list').find('li.defaultAddr').attr('address-num');
                //使用request()函数可以获取到当前访问地址中的参数信息,是非常方便的,但这由于这里传递的cart_id参数有可能是3,6这种形式,js获取到之后会把这当作数字,控制台中会报错,所以要在request('cart_id')外面加上双引号,指明这是一个字符串
                let cart_id = "{{request('cart_id')}}";
                // 只需要上面的数据就可以,发送异步请求了
                //route('center.order.store')返回的是一个url地址.下面指定使用axios发送post异步请求,请求地址要是一个字符串,所以要在route('center.order.store')外侧加上引号
                axios.post("{{route('center.order.store')}}", {
                    "addressTo": addressTo, "cart_id": cart_id
                }).then((res) => {
                    //console.log(res);return false;
                    // 如果异步请求返回成功就跳转到支付页面.到这里我才明白这里为啥要发异步了,因为这里想要实现用户点击一次提交保存订单后台要完成两个动作,首先要创建相应的订单,产生订单编号,然后带着刚刚产生的订单号跳转到支付页面完成支付
                    res = res.data;
                    // 如果执行没有问题,返回正确数据就带上订单号跳转到支付页面
                    if (res.valid) {
                        location.href = '/orderPay/' + res.order_num;
                    }
                })
            })
        }

        //通过直接购买到这个页面提交订单的时候执行这个方法
        $(function () {
            $("#direct_store_order").click(function () {
                //首先判断用户有没有选择地址和支付方式
                let item_kuaidi = $(".op_express_delivery_hot").find('li.selected').length;
                let item_pay = $(".pay-list").find('li.selected').length;
                if (item_kuaidi==0 || item_pay==0){
                    //进入到这个判断体中说明没有选择快递或者支付方式
                    require(['hdjs'],function (hdjs) {
                        hdjs.swal({
                            text: "请选择快递和支付方式",
                            button:false,
                            icon:"warning"
                        })
                    })
                    // 阻止后续提交订单事件
                    return false;
                }
                //先获取到用户选中的地址信息
                let addressTo = $("#address-list").find("li.defaultAddr").attr('address-num');
                /*
                $.post("{{--route('center.order.store')--}}",{
                    "addressTo":addressTo
                },function (res) {
                },"json")
                */
                require(['hdjs', 'axios'], function (hdjs, axios) {
                    axios.post("{{route('center.order.store')}}", {
                        "addressTo": addressTo,"carts":@json($carts->first()->toArray())
                    }).then((res) => {
                            if (res.data.valid) {
                                location.href = '/orderPay/' + res.data.order_num;
                            }
                    })
                })
            })
        })
    </script>
    <div class="clear"></div>
</div>

</div>
{{--网站公共底部--}}
@include('site.layouts._bottom')
<div class="theme-popover-mask"></div>
<div class="theme-popover">

    <!--标题 -->
    <div class="am-cf am-padding">
        <div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">新增地址</strong> /
            <small>Add address</small>
        </div>
    </div>
    <hr/>

    <div class="am-u-md-12">
        <form class="am-form am-form-horizontal">

            <div class="am-form-group">
                <label for="user-name" class="am-form-label">收货人</label>
                <div class="am-form-content">
                    <input type="text" id="user-name" placeholder="收货人">
                </div>
            </div>

            <div class="am-form-group">
                <label for="user-phone" class="am-form-label">手机号码</label>
                <div class="am-form-content">
                    <input id="user-phone" placeholder="手机号必填" type="email">
                </div>
            </div>

            <div class="am-form-group">
                <label for="user-phone" class="am-form-label">所在地</label>
                <div class="am-form-content address">
                    <select data-am-selected>
                        <option value="a">浙江省</option>
                        <option value="b">湖北省</option>
                    </select>
                    <select data-am-selected>
                        <option value="a">温州市</option>
                        <option value="b">武汉市</option>
                    </select>
                    <select data-am-selected>
                        <option value="a">瑞安区</option>
                        <option value="b">洪山区</option>
                    </select>
                </div>
            </div>

            <div class="am-form-group">
                <label for="user-intro" class="am-form-label">详细地址</label>
                <div class="am-form-content">
                    <textarea class="" rows="3" id="user-intro" placeholder="输入详细地址"></textarea>
                    <small>100字以内写出你的详细地址{{asset('/org/site')}}.</small>
                </div>
            </div>

            <div class="am-form-group theme-poptit">
                <div class="am-u-sm-9 am-u-sm-push-3">
                    <div class="am-btn am-btn-danger">保存</div>
                    <div class="am-btn am-btn-danger close">取消</div>
                </div>
            </div>
        </form>
    </div>

</div>

<div class="clear"></div>
</body>

</html>
